package com.czxy.tanhua.service;

import com.czxy.tanhua.autoconfig.template.OssTemplate;
import com.czxy.tanhua.dubbo.api.MovementApi;
import com.czxy.tanhua.dubbo.api.UserInfoApi;
import com.czxy.tanhua.entity.UserInfo;
import com.czxy.tanhua.mongo.Movement;
import com.czxy.tanhua.vo.MovementVo;
import com.czxy.tanhua.vo.PostUpdateVo;
import com.czxy.tanhua.vo.PublicPageVo;
import com.czxy.tanhua.vo.ResultList;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/4/8 13:46
 */
@Service
public class MovementService {
    @DubboReference
    private MovementApi movementApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Resource
    private OssTemplate ossTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    public ResultList findmovements(Long userId, PublicPageVo publicPageVo) {
        ResultList findmovements = movementApi.findmovements(userId,publicPageVo);
        return findmovements;
    }

    public void fabudongtai(List<MultipartFile> imageContent, PostUpdateVo postUpdateVo, Long userId, String mobile) {
        for (MultipartFile multipartFile : imageContent) {
            String upload = ossTemplate.upload(multipartFile);
            System.out.println(upload);
            postUpdateVo.getImages().add(upload);
        }
        movementApi.fabudongtai(postUpdateVo,userId,mobile);
    }

    public ResultList friendmovements(PublicPageVo publicPageVo, Long userId) {
        ResultList friendmovements = movementApi.friendmovements(publicPageVo, userId);
        List<Movement> items = friendmovements.getItems();
        List<MovementVo> movements = new ArrayList<>();
        for (Movement movement : items) {
            UserInfo userInfo = userInfoApi.findUser(movement.getUserId());
            extracted(userId, movements, movement, userInfo);
        }
        friendmovements.setItems(movements);
        return friendmovements;
    }

    private void extracted(Long userId, List<MovementVo> movements, Movement movement, UserInfo userInfo) {
        String keyname = "MOVEMENTS_INTERACT_KEY"+ movement.getId();
        String hashKey = "MOVEMENTS_LIKE_HASHKEY" + userId;
        String keylove = "MOVEMENTS_LOVE_KEY"+ movement.getId();
        String hashKeylove = "MOVEMENTS_LOVE_HASHKEY" + userId;
        if (stringRedisTemplate.opsForHash().hasKey(keyname,hashKey) || stringRedisTemplate.opsForHash().hasKey(keylove,hashKeylove)){
            MovementVo movementVo = new MovementVo(movement.getId().toString(),
                    userInfo.getId(), userInfo.getAvatar(), userInfo.getNickname(), userInfo.getGender(), Integer.valueOf(userInfo.getAge()),
                    userInfo.getTags().split(","), movement.getTextContent(), movement.getMedias(),"500米", movement.getCreated().toString()
                    , movement.getLikeCount(), movement.getCommentCount(), movement.getLoveCount(),1,1);
            movements.add(movementVo);
        }else {
            MovementVo movementVo = new MovementVo(movement.getId().toString(),
                    userInfo.getId(), userInfo.getAvatar(), userInfo.getNickname(), userInfo.getGender(), Integer.valueOf(userInfo.getAge()),
                    userInfo.getTags().split(","), movement.getTextContent(), movement.getMedias(),"500米", movement.getCreated().toString()
                    , movement.getLikeCount(), movement.getCommentCount(), movement.getLoveCount(),0,0);
            movements.add(movementVo);
        }
    }

    public ResultList recommendmovements(PublicPageVo publicPageVo, Long userId) {
        //处理redis中数据
        String key = "MOVEMENTS_RECOMMEND_"+userId;
        //找到推荐评论的推荐pid
        String pids = stringRedisTemplate.opsForValue().get(key);
        //切割处理数据
        String[] pidList = pids.split(",");
        //查找对应的评论和userid
        ResultList recommendmovements = movementApi.recommendmovements(pidList,publicPageVo);
        List<Movement> items = recommendmovements.getItems();
        //传入用户APi
        Map<Long, UserInfo> userInfoMap = userInfoApi.findUserByids(items.stream().map(movement -> movement.getUserId()).collect(Collectors.toList()));

        List<MovementVo> movementVoList = new ArrayList<>();
        //处理返回数据
        for (Movement movement : items) {
            UserInfo userInfo = userInfoMap.get(movement.getUserId());
            extracted(userId, movementVoList, movement, userInfo);
        }
        recommendmovements.setItems(movementVoList);
        return recommendmovements;
    }

    public MovementVo findmovement(ObjectId id) {
        //处理评论
        Movement movement = movementApi.findmovement(id);

        UserInfo userInfo = userInfoApi.findUser(movement.getUserId());

        MovementVo movementVo = new MovementVo(movement.getId().toString(),
                userInfo.getId(),userInfo.getAvatar(),userInfo.getNickname(),userInfo.getGender(),Integer.valueOf(userInfo.getAge()),
                userInfo.getTags().split(","),movement.getTextContent(),movement.getMedias(),"500米",movement.getCreated().toString()
                ,movement.getLikeCount(),movement.getCommentCount(),movement.getLoveCount(),0,0);
        return movementVo;
    }

    public Movement likemovement(ObjectId id,Long userId) {
        Movement movement = movementApi.likemovement(id);
        //将当前用户的点赞状态存入redis中
        String key = "MOVEMENTS_INTERACT_KEY"+movement.getId();
        String hashKey = "MOVEMENTS_LIKE_HASHKEY" + userId;
        stringRedisTemplate.opsForHash().put(key,hashKey,"1");
        return movement;
    }

    public Movement dislikemovement(ObjectId id, Long userId) {
        Movement movement = movementApi.dislikemovement(id);
        //将当前用户的点赞状态存入redis中
        String key = "MOVEMENTS_INTERACT_KEY"+movement.getId();
        String hashKey = "MOVEMENTS_LIKE_HASHKEY" + userId;
        stringRedisTemplate.opsForHash().delete(key,hashKey);
        return movement;
    }

    public Movement loveemovement(ObjectId id, Long userId) {
        Movement movement = movementApi.loveemovement(id);
        //将当前用户的点赞状态存入redis中
        String key = "MOVEMENTS_LOVE_KEY"+movement.getId();
        String hashKey = "MOVEMENTS_LOVE_HASHKEY" + userId;
        stringRedisTemplate.opsForHash().put(key,hashKey,"1");
        return movement;
    }

    public Movement unlovemovement(ObjectId id, Long userId) {
        Movement movement = movementApi.unlovemovement(id);
        //将当前用户的点赞状态存入redis中
        String key = "MOVEMENTS_LOVE_KEY"+movement.getId();
        String hashKey = "MOVEMENTS_LOVE_HASHKEY" + userId;
        stringRedisTemplate.opsForHash().delete(key,hashKey);
        return movement;
    }
}
